//@author Bayu Prawira

//Check in Check out date
function day_name(d){
	var weekday = new Array(7);
	weekday[0] = "Sunday";
	weekday[1] = "Monday";
	weekday[2] = "Tuesday";
	weekday[3] = "Wednesday";
	weekday[4] = "Thursday";
	weekday[5] = "Friday";
	weekday[6] = "Saturday";
	
	var n = weekday[d.getDay()];
	
	return n;
}

function night(){
	var start = $('#date_from').datepicker('getDate');
	var end   = $('#date_to').datepicker('getDate');
	var days   = (end - start)/1000/60/60/24;
	
	if(start != null && end != null){
		var night = '';
		for(var i=1;i<31;i++){
			if(days == i){
				var selected = 'selected="selected"';
			} else {
				var selected = '';
			}
			
			night += '<option value="'+i+'" '+selected+'>'+i+'</option>';
		}
		$( "#night" ).html(night);
	}
}

function dateRange(){
	var dateFrom = $('#date_from').datepicker("getDate");
	
	if(dateFrom != null){
		var night = $('#night').val();
		var newDate = new Date(dateFrom.getFullYear(), dateFrom.getMonth(),dateFrom.getDate()+ parseInt(night));
		$('#date_to').datepicker('setDate', newDate);
		
		// set day
		$('#day_date_to').html(day_name(newDate));
			
		var dateTo = $('#date_to').datepicker("getDate");
		var maxDate = new Date(dateTo.getFullYear(), dateTo.getMonth(),dateTo.getDate() - 1);
		$( "#date_from" ).datepicker( "option", "maxDate", maxDate );
	} else {
		alert('Please choose your check in date!!');
	}
}

$(function() {
	if($( "#date_from" ).length){
		$( "#date_from" ).datepicker({
			showOn: "button",
			dateFormat: "dd/mm/yy",
			buttonImage: "images/calendar.gif",
			buttonImageOnly: true,
			nextText: "",
			prevText: "",
			minDate: +1,
			onSelect: function( selectedDate ) {
				var dateFrom = $('#date_from').datepicker("getDate");
				var minDate = new Date(dateFrom.getFullYear(), dateFrom.getMonth(),dateFrom.getDate() + 1);
				var maxDate = new Date(dateFrom.getFullYear(), dateFrom.getMonth(),dateFrom.getDate() + 30);
				$( "#date_to" ).datepicker( "option", "minDate", minDate );
				$( "#date_to" ).datepicker( "option", "maxDate", maxDate );
				
				// set day
				$('#day_date_from').html(day_name(dateFrom));
				
				//night();
				dateRange();
			}
		});
		
		var getDateFrom = $('#date_from').datepicker("getDate");
		var minDateTo = new Date(getDateFrom.getFullYear(), getDateFrom.getMonth(),getDateFrom.getDate() + 1);
	
		$( "#date_to" ).datepicker({
			showOn: "button",
			dateFormat: "dd/mm/yy",
			buttonImage: "images/calendar.gif",
			buttonImageOnly: true,
			nextText: "",
			prevText: "",
			minDate: minDateTo,
			onSelect: function( selectedDate ) {
				var dateTo = $('#date_to').datepicker("getDate");
				var maxDate = new Date(dateTo.getFullYear(), dateTo.getMonth(),dateTo.getDate() - 1);
				$( "#date_from" ).datepicker( "option", "maxDate", maxDate );
				
				// set day
				$('#day_date_to').html(day_name(dateTo));
				
				// set night
				night();
			}
		});
	}
});

$(document).ready(function() {
	// first load get day
	if($('#date_from').length){
		var dateFrom = $('#date_from').datepicker("getDate");
		var dateTo = $('#date_to').datepicker("getDate");
		// set day
		$('#day_date_from').html(day_name(dateFrom));
		$('#day_date_to').html(day_name(dateTo));
	}				   
});


// Hotel Google Map
var map;
var global_markers = [];    
var markers = [[32.86125,-96.89041,'MCM Elegante Hotel & Suites','http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_57_t.jpg']];

var infowindow = new google.maps.InfoWindow({});

function initialize() {
	if(document.getElementById("hotel-map")){
		var lat = parseFloat(markers[0][0]);
		var lng = parseFloat(markers[0][1]);
		geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(lat, lng);
		var myOptions = {
			zoom: 15,
			center: latlng,
			streetViewControl: false,
			mapTypeControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		map = new google.maps.Map(document.getElementById("hotel-map"), myOptions);
		addMarker();
	}
}

function addMarker() {
	for (var i = 0; i < markers.length; i++) {
		// obtain the attribues of each marker
		var lat = parseFloat(markers[i][0]);
		var lng = parseFloat(markers[i][1]);
		var hotel_name = markers[i][2];
		var hotel_image = markers[i][3];
		var hotel_address = markers[i][4];
		var hotel_description = markers[i][5];

		var myLatlng = new google.maps.LatLng(lat, lng);

		var contentString = '<table cellpadding="0" cellspacing="0" width="300">';
		contentString += '<tr><td width="150"><img src="'+hotel_image+'" width="150" style="margin:0 10px 10px 0;" /></td>';
		contentString += '<td style="vertical-align:top;"><div class="title">'+hotel_name+'</div><div class="address">'+hotel_address+'</div><div><?php echo $star; ?></div></td>';
		contentString += '</tr><tr><td colspan="2">'+hotel_description+'</td></tr></table>';

		var marker = new google.maps.Marker({
			position: myLatlng,
			map: map,
			title: lat + " , " + lng + " | " + hotel_name
		});

		marker['infowindow'] = contentString;

		global_markers[i] = marker;

		google.maps.event.addListener(global_markers[i], 'click', function() {
			infowindow.setContent(this['infowindow']);
			infowindow.open(map, this);
		});
	}
}

window.onload = initialize;

// Accordion hotel detail
$(function() {
	$("#accordion").accordion({ header: "h4", collapsible: true, active: false });
});

// Global Search Add Room
function add_room(){
	var next_room = parseInt($('#current_room').val())+1;
	$('#rm_'+next_room).show();
	$('#current_room').val(next_room);
	if(next_room == 4){
		$('#add_room_button').hide();
	}
}

// Global Search Remove Room
function remove_room(){
	var current_room = $('#current_room').val();
	$('#rm_'+current_room).hide();
	$('#current_room').val(parseInt(current_room)-1);
	if((parseInt(current_room)-1) < 4){
		$('#add_room_button').show();
	}
}

function child_age(id){
	var value = $('#child-'+id).val();
	
	if(value == 0){
		$('#age_'+id).hide();
	} else {
		$('#age_'+id).show();
		$('.age_'+id).hide();
		
		for(var i=1; i<=value; i++){
			$('#age_'+id+'_'+i).show();
		}
	}
}